use std::error::Error;
use bytes::Bytes;
use crate::{CmdResponse, Get, Set, Storage};
use crate::service::CmdService;

impl CmdService for Get {
    fn execute(self, store: &impl Storage) -> CmdResponse {
        // 从存储中取得数据并返回
        match store.get(&self.key) {
            Ok(Some(value)) => value.into(),
            Ok(None) => "NOT FOUND".into(),
            Err(e) => e.into(),
        }
    }
}


impl CmdService for Set {
    fn execute(self, store: &impl Storage) -> CmdResponse {
        // 存储数据
        match store.set(&self.key, self.value) {
            Ok(Some(value)) => value.into(),
            Ok(None) => "SET FAILED".into(),
            Err(e) => e.into(),
        }
    }
}